package com.lz.zookeeper;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;

import com.lz.common.util.GetCfg;

public class ZooKeeperFactory {
	
    public static final String ZK_ADDRESS = GetCfg.getDefaultValue("Zk_Address", "192.168.10.120:2181");
    public static final int MAX_RETRIES = 3;
    public static final int BASE_SLEEP_TIMEMS = 3000;
 
    public static CuratorFramework get() {
    	return get(ZK_ADDRESS);
    }
    
    public static CuratorFramework get(String address) {
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(BASE_SLEEP_TIMEMS, MAX_RETRIES);
        CuratorFramework client = CuratorFrameworkFactory.builder()
                .connectString(address)
                .retryPolicy(retryPolicy)
                .build();
        client.start();
        return client;
    }
}
